package com.hegongda.okcmall.product.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.hegongda.common.to.SkuStockVo;
import com.hegongda.common.to.SpuBoundsTo;
import com.hegongda.common.to.es.Attrs;
import com.hegongda.common.to.es.SkuEsModel;
import com.hegongda.okcmall.product.entity.*;
import com.hegongda.okcmall.product.fegin.CouponService;
import com.hegongda.okcmall.product.service.*;
import com.hegongda.okcmall.product.vo.BaseAttrs;
import com.hegongda.okcmall.product.vo.Bounds;
import com.hegongda.okcmall.product.vo.Skus;
import com.hegongda.okcmall.product.vo.SpuInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.R;



/**
 * spu信息
 *
 * @author Liuweidong
 * @email Liuweidong@gmail.com
 * @date 2021-01-31 17:57:14
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private SpuImagesService spuImagesService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private CouponService couponService;





    // product/spuinfo/18/up 商品上架
    /*
       商品上架：将上架的商品信息同步到 elasticsearch 便于以后的检索
       一个spu下可能会存在多个sku
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId){

        spuInfoService.up(spuId);
        return R.ok();
    }

    /**
     * 查询spu基本信息
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.querySpuByCondition(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存spu的信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuInfoVo spuInfoVo){
		// 保存Spu的基本信息
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuInfoVo,spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        spuInfoService.save(spuInfoEntity);
        // 保存Spu的图片描述信息
        List<String> decript = spuInfoVo.getDecript();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescEntity.setDecript(String.join(",",decript));
        spuInfoDescService.saveDesc(spuInfoDescEntity);
        // 保存spu的图集信息
        List<String> images = spuInfoVo.getImages();
        spuImagesService.saveImage(spuInfoEntity.getId(), images);
        // 保存spu的Attr属性
        List<BaseAttrs> baseAttrs = spuInfoVo.getBaseAttrs();
        productAttrValueService.saveSpuAttrs(spuInfoEntity.getId(), baseAttrs);
        // 远程保存积分的信息：调用coupon服务
        SpuBoundsTo spuBoundsTo = new SpuBoundsTo();
        Bounds bounds = spuInfoVo.getBounds();
        BeanUtils.copyProperties(bounds,spuBoundsTo);
        spuBoundsTo.setSpuId(spuInfoEntity.getId());
        couponService.save(spuBoundsTo);
        // 保存sku的信息
        List<Skus> skus = spuInfoVo.getSkus();
        spuInfoService.saveSkuInfo(spuInfoVo.getBrandId(), spuInfoVo.getCatalogId(),spuInfoEntity.getId(), skus);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
